package com.quanyan.riding.task;

import com.github.ltsopensource.core.domain.Job;
import com.github.ltsopensource.jobclient.JobClient;
import com.github.ltsopensource.jobclient.domain.Response;
import com.quanyan.redisCluster.utils.RedisConstants;
import com.quanyan.redisCluster.utils.RedisKey;
import com.quanyan.riding.component.cache.MyRedisTemplate;
import com.quanyan.riding.constans.Constans;
import com.quanyan.riding.util.CommonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author xian-guo
 */

public class TimerTaskJob {

    private static final Logger LOGGER = LoggerFactory.getLogger(TimerTaskJob.class);

    @Autowired
    private JobClient jobClient;

    @Autowired
    private MyRedisTemplate myRedisTemplate;

    public void executeTask() {

        //定时删除路书上用户job
        submitDelPathUserJob();

        //删除超过72小时的车队
        submitDelBike();

        //测试版本先生成5000个随机六位数，用户车队口令
        productCommand();
    }

    /**
     * 定时删除路书上用户job
     */
    void submitDelPathUserJob(){
        Job job = new Job();
        job.setTaskId("task_runRiding_delete_pathUser");//每个任务必须指定唯一
        job.setParam("type", "typeA");//执行多个任务，传递参数
        job.setTaskTrackerNodeGroup("taskTracker_runRiding");//每个模块指定不同的名称，但同一个模块不同任务必须相同
        job.setNeedFeedback(true);
        job.setReplaceOnExist(true);        // 当任务队列中存在这个任务的时候，是否替换更新
        job.setRepeatCount(-1);
        job.setRepeatInterval(30 * 60 * 1000L);
        Response response = jobClient.submitJob(job);
        LOGGER.info("------任务是否提交成功----->>>>>>>>>>>>>>>>"+response);
    }

    /**
     * 每隔一分钟定时删除超过72小时的车队
     */
    void submitDelBike(){
        Job job = new Job();
        job.setTaskId("task_runRiding_delete_bike");//每个任务必须指定唯一
        job.setParam("type", "typeB");//执行多个任务，传递参数
        job.setTaskTrackerNodeGroup("taskTracker_runRiding");//每个模块指定不同的名称，但同一个模块不同任务必须相同
        job.setNeedFeedback(true);
        job.setReplaceOnExist(true);        // 当任务队列中存在这个任务的时候，是否替换更新
        job.setRepeatCount(-1);
        job.setRepeatInterval(60 * 1000L);
        Response response = jobClient.submitJob(job);
        LOGGER.info("------任务是否提交成功----->>>>>>>>>>>>>>>>"+response);
    }

    /**
     * 生成车队口令
     */
    void productCommand(){
        RedisKey key = new RedisKey(RedisConstants.RUN_RIDING_CACHE_PREFIX, Constans.GET_BIKE_COMMAND);
        List<Integer> commandList= (List<Integer>) myRedisTemplate.get(key);
        if (null ==commandList){
            commandList= CommonUtil.getRandomCommand(commandList);
            myRedisTemplate.set(key,commandList);
            LOGGER.error("-------------------重新生成了车队口令--------------------------");
        }
    }


}
